package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckInfoDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckInfo;
import com.itheima.service.CheckInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = CheckInfoService.class)
@Transactional
public class CheckInfoServiceImpl implements CheckInfoService {
    @Autowired
    private CheckInfoDao checkInfoDao;
    @Autowired
    private OrderDao orderDao;

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Map> pageResult = orderDao.findPage(queryString);
        long total = pageResult.getTotal();
        List<Map> daoResult = pageResult.getResult();
        List<Map> resultList = new ArrayList<Map>();
        for (Map map : daoResult) {
            if("已到诊".equals(map.get("orderStatus"))){
                resultList.add(map);
            }
        }
        return new PageResult(total,resultList);
    }

    @Override
    public List<Map<String,Object>> findAllCheckgroupAndCheckitem(Integer orderId) {
        //先把检查组id和name查出来
        List<Map<String,Object>> checkgroupMapList = checkInfoDao.findCheckgroupByOrderId(orderId);
        for (Map map : checkgroupMapList) {
            //根据检查组id查检查项，塞到里面
            Integer checkgroupId = Integer.parseInt(map.get("checkgroupId").toString());
            Map<String,Integer> parameterMap = new HashMap<>();
            parameterMap.put("orderId",orderId);
            parameterMap.put("checkgroupId",checkgroupId);
            List<Map<String,Object>> checkitemMapList= checkInfoDao.findCheckitemByOrderIdAndCheckgroupId(parameterMap);
            map.put("checkitemMap",checkitemMapList);
        }
        return checkgroupMapList;
    }

    @Override
    public void update(List<Map<String, Object>> checkgroupList) {
        for (Map<String, Object> checkgroup : checkgroupList) {
            Integer orderId = Integer.parseInt(checkgroup.get("orderId").toString());
            Integer checkgroupId = Integer.parseInt(checkgroup.get("checkgroupId").toString());
            List<Map> checkitemList = (List<Map>) checkgroup.get("checkitemMap");
            for (Map checkitem : checkitemList) {
                Integer checkitemId = Integer.parseInt(checkitem.get("checkitemId").toString());
                String content = (String)checkitem.get("content");
                Map<String,Object> parameterMap = new HashMap<>();
                parameterMap.put("orderId",orderId);
                parameterMap.put("checkgroupId",checkgroupId);
                parameterMap.put("checkitemId",checkitemId);
                parameterMap.put("content",content);
                checkInfoDao.update(parameterMap);
            }
        }
    }

    @Override
    public void complete(List<Map<String, Object>> checkgroupList) {
        Integer orderId=null;
        //修改值
        for (Map<String, Object> checkgroup : checkgroupList) {
            orderId = Integer.parseInt(checkgroup.get("orderId").toString());
            Integer checkgroupId = Integer.parseInt(checkgroup.get("checkgroupId").toString());
            List<Map> checkitemList = (List<Map>) checkgroup.get("checkitemMap");
            for (Map checkitem : checkitemList) {
                Integer checkitemId = Integer.parseInt(checkitem.get("checkitemId").toString());
                String content = (String)checkitem.get("content");
                Map<String,Object> checkinfoMap = new HashMap<>();
                checkinfoMap.put("orderId",orderId);
                checkinfoMap.put("checkgroupId",checkgroupId);
                checkinfoMap.put("checkitemId",checkitemId);
                checkinfoMap.put("content",content);
                checkInfoDao.update(checkinfoMap);
            }
        }
        //修改order表中的状态为已完成，结束日期为当前时间
        Map<String,Object> orderMap = new HashMap<String,Object>();
        orderMap.put("orderId",orderId);
        orderMap.put("orderStatus","已完成");
        orderMap.put("endTime",new Date());
        orderDao.update(orderMap);

    }

    @Override
    public PageResult reportFindPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Map> pageResult = orderDao.findPage(queryString);
        long total = pageResult.getTotal();
        List<Map> daoResult = pageResult.getResult();
        List<Map> resultList = new ArrayList<Map>();
        for (Map map : daoResult) {
            if("已完成".equals(map.get("orderStatus"))){
                resultList.add(map);
            }
        }
        return new PageResult(total,resultList);
    }

    @Override
    public Map findByorderId(Integer orderId) {
        Map map = orderDao.findByOrderId(orderId);
        return map;
    }
}
